package com.atguigu.controller;


import com.atguigu.entity.BaseBrand;
import com.atguigu.result.RetVal;
import com.atguigu.service.BaseBrandService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.io.FilenameUtils;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author GuoJH
 * @since 2021-08-24
 */
@RestController
@RequestMapping("/product/brand")
public class BrandController {
    //从yml配置文件中获取虚拟机地址
    @Value("${fastdfs.prefix}")
    private String fastdfsPrefix;

    @Autowired
    private BaseBrandService brandService;
    //品牌列表分页
    @GetMapping("/queryBrandByPage/{current}/{limit}")
    public RetVal queryProductSpuByPage(@PathVariable Long current,
                                        @PathVariable Long limit){
        Page<BaseBrand> brandPage = new Page<>(current, limit);
        QueryWrapper<BaseBrand> wrapper = new QueryWrapper<>();
        //可做个排序
        wrapper.orderByDesc("id");
        brandService.page(brandPage, wrapper);
        return RetVal.ok(brandPage);
    }
    //添加品牌
    @PostMapping
    public RetVal saveBrand(@RequestBody BaseBrand baseBrand){
        brandService.save(baseBrand);
        return RetVal.ok();
    }
    //先通过id查询品牌
    @GetMapping("{brandId}")
    public RetVal getBrand(@PathVariable Long brandId){
        BaseBrand brand = brandService.getById(brandId);
        return RetVal.ok(brand);
    }
    //修改品牌
    @PutMapping
    public RetVal updateBrand(@RequestBody BaseBrand baseBrand){
        brandService.updateById(baseBrand);
        return RetVal.ok();
    }
    //删除品牌
    @DeleteMapping("{brandId}")
    public RetVal deleteBrand(@PathVariable Long brandId){
        brandService.removeById(brandId);
        return RetVal.ok();
    }
    //查询所有
    @GetMapping("/getAllBrand")
    public RetVal getAllBrand(){
        List<BaseBrand> list = brandService.list(null);
        return RetVal.ok(list);
    }
    //品牌添加  图片上传
    @RequestMapping("/fileUpload")
    public RetVal fileUpload(MultipartFile file) throws Exception {
        //写一个配置 告诉fastDFS存储到哪里
        String configFilePath = this.getClass().getResource("/tracker.conf").getFile();
        //初始化文件
        ClientGlobal.init(configFilePath);
        //创建tracker server服务器
        TrackerClient trackerClient = new TrackerClient();
        //与storage server 建立链接
        TrackerServer connection = trackerClient.getConnection();
        //创建storage server服务器
        StorageClient1 storageClient1 = new StorageClient1(connection, null);
        //调用方法 文件上传
        String filename = file.getOriginalFilename();
        String extension = FilenameUtils.getExtension(filename);//获取文件的后缀
        //返回图片访问的路径
        String path = storageClient1.upload_appender_file1(file.getBytes(), extension, null);
        return RetVal.ok(fastdfsPrefix+path);
    }
    //通过品牌的id获取品牌的信息  用于远程调用 展示商品上架信息
    @GetMapping("/getBrandById/{brandId}")
    public BaseBrand getBrandById(@PathVariable Long brandId){
        BaseBrand brand = brandService.getById(brandId);
        return brand;
    }
}

